//
//  QDAddCaseController.m
//  QuickDoctor
//
//  Created by ycw on 2016/12/25.
//  Copyright © 2016年 Eric. All rights reserved.
//

#import "QDAddCaseController.h"
#import "QDAddCeseCell1.h"
#import "QDAddCaseCell2.h"
#import "QDAddCaseCell3.h"
#import "QDAddCaseCell4.h"
#import "QDCaseTypeController.h"
#import "QDSubsectionController.h"
#import "QDCaseModel.h"

static NSString *cellID1 = @"cell1";

static NSString *cellID2 = @"cell2";

static NSString *cellID3 = @"cell3";

static NSString *cellID4 = @"cell4";


@interface QDAddCaseController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIGestureRecognizerDelegate>

@property(weak,nonatomic) QDAddCeseCell1 *cell1;

@property(weak,nonatomic) QDAddCaseCell2 *cell2;

@property(weak,nonatomic) QDAddCaseCell3 *cell3;

@property(weak,nonatomic) QDAddCaseCell4 *cell4;

@property(weak,nonatomic) UIImage *img;

@end

@implementation QDAddCaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"添加病例";
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightButton)];
    
    self.navigationItem.rightBarButtonItem = rightButton;
    
    [self setUpUI];
    
    
}

#pragma mark
#pragma mark - 搭建界面
- (void) setUpUI
{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    tableView.estimatedRowHeight = 150;
    
    tableView.rowHeight = UITableViewAutomaticDimension;
    
    [tableView registerClass:[QDAddCeseCell1 class] forCellReuseIdentifier:cellID1];
    
    [tableView registerClass:[QDAddCaseCell2 class] forCellReuseIdentifier:cellID2];
    
    [tableView registerClass:[QDAddCaseCell3 class] forCellReuseIdentifier:cellID3];
    
    [tableView registerClass:[QDAddCaseCell4 class] forCellReuseIdentifier:cellID4];
    
    
}

#pragma mark
#pragma mark - 实现数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 2;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        QDAddCeseCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:cellID1 forIndexPath:indexPath];
        
        cell1.textLabel.text = @"疾病类型";
        
        cell1.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        self.cell1 = cell1;
        
        return cell1;
        
    }else if(indexPath.section == 0 && indexPath.row == 1){
        
        QDAddCaseCell4 *cell4 = [tableView dequeueReusableCellWithIdentifier:cellID1 forIndexPath:indexPath];
        
        cell4.textLabel.text = @"疾病细分";
        
        cell4.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        self.cell4 = cell4;
        
        return cell4;
    }else if (indexPath.section == 1 && indexPath.row == 0){
        
        QDAddCaseCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:cellID2 forIndexPath:indexPath];
        
        self.cell2 = cell2;
        
        return cell2;
        
    }else{
        
        QDAddCaseCell3 *cell3 = [tableView dequeueReusableCellWithIdentifier:cellID3 forIndexPath:indexPath];
        
        [cell3.imageButton addTarget:self action:@selector(clickImageView) forControlEvents:UIControlEventTouchUpInside];
        
        self.cell3 = cell3;
        

        return cell3;
    }
    
    return nil;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        QDCaseTypeController *typeC = [[QDCaseTypeController alloc] init];
        
        typeC.typeBlock = ^(NSString *typetext){
            
            self.cell1.label.text = typetext;
        };
        
        [self.navigationController pushViewController:typeC animated:YES];
        
    }else if (indexPath.section == 0 && indexPath.row == 1){
        
        QDSubsectionController *subC = [[QDSubsectionController alloc] init];
        
        subC.subBlock = ^(NSString *subText){
            
            self.cell4.label.text = subText;
        
        };
        
        [self.navigationController pushViewController:subC animated:YES];
        
    }
    
}

#pragma mark
#pragma mark - 实现点击图片方法
- (void)clickImageView
{
    
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
    
    // sourceType
    pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    
    pickerController.delegate = self;
    
    
    // 展示控制器
    [self presentViewController:pickerController animated:YES completion:nil];
    
}


#pragma mark
#pragma mark - imagePicker的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    NSLog(@"%@",info);
    // 获取到选中的图片
    UIImage *image = info[@"UIImagePickerControllerOriginalImage"];
    
    self.img = image;
    
    [self.cell3.imageButton setImage:image forState:UIControlStateNormal];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}



#pragma mark
#pragma mark - 点击提交按钮

- (void) clickRightButton
{
    
    QDCaseModel *model = [[QDCaseModel alloc] init];
    
    model.illnessType = self.cell1.label.text;
    
    model.illnessSub = self.cell4.label.text;
    
    model.illnessDesc = self.cell2.txfText.text;
    
    NSData *data = UIImageJPEGRepresentation(self.img, 0.5);
    
//    NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
//    
    model.illnessImage = data;
    
    //获取当前时间
    NSDate *currentDate = [NSDate date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    
    model.illnessTime = dateString;
    
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    
    NSString *filePath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"ycw_illnessHistory.plist"]];
    
    //归档@"ycw_illnessHistory.plist"
    NSMutableArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    
    if (array == nil) {
        
        array = [NSMutableArray array];
        
    }
    
    [array insertObject:model atIndex:0];
    
    [NSKeyedArchiver archiveRootObject:array toFile:filePath];
    
    if (_completeBlock) {
        
        _completeBlock(model);
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}







@end
